/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co

 */
package co.yixiang.mall.biz.shop.controller;

import co.yixiang.common.core.exception.UtilException;
import co.yixiang.common.core.utils.location.GetTencentLocationVO;
import co.yixiang.common.log.annotation.Log;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.biz.shop.service.YxSystemStoreService;
import co.yixiang.mall.common.shop.domain.YxSystemStore;
import co.yixiang.mall.common.shop.dto.YxSystemStoreDto;
import co.yixiang.mall.common.shop.dto.YxSystemStoreQueryCriteria;
import co.yixiang.mall.common.utils.GouldUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

/**
* @author hupeng
* @date 2020-03-03
*/
@Api(tags = "门店管理")
@RestController
@RequestMapping("/yxSystemStore")
public class SystemStoreController {

    private final YxSystemStoreService yxSystemStoreService;
    private final IGenerator generator;
    public SystemStoreController(YxSystemStoreService yxSystemStoreService, IGenerator generator) {
        this.yxSystemStoreService = yxSystemStoreService;
        this.generator = generator;
    }


    @Log("所有门店")
    @ApiOperation("所有门店")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:list')")
    public ResponseEntity<Object>  getAll(YxSystemStoreQueryCriteria criteria) {
        return new ResponseEntity<>(yxSystemStoreService.queryAll(criteria),HttpStatus.OK);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:list')")
    public void download(HttpServletResponse response, YxSystemStoreQueryCriteria criteria) throws IOException {
        yxSystemStoreService.download(generator.convert(yxSystemStoreService.queryAll(criteria), YxSystemStoreDto.class), response);
    }

    @GetMapping
    @Log("查询门店")
    @ApiOperation("查询门店")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:list')")
    public ResponseEntity<Object> getYxSystemStores(YxSystemStoreQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxSystemStoreService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping(value = "/getL")
    @Log("获取经纬度")
    @ApiOperation("获取经纬度")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:getl')")
    public ResponseEntity<GetTencentLocationVO> create(@Validated @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String addr = jsonObject.getString("addr");
//        GetTencentLocationVO locationVO = LocationUtils.getLocation(addr);
        String lonLat = GouldUtil.getLonLat(addr);
       /* if (StringUtils.isEmpty(lonLat)){
            throw new UtilException("请输入");
        }*/
        String[] split = lonLat.split(",");
        double lng = Double.parseDouble(split[0]);
        double lat = Double.parseDouble(split[1]);
        GetTencentLocationVO.ResultBean resultBean = new GetTencentLocationVO.ResultBean();
        GetTencentLocationVO.ResultBean.LocationBean locationBean = new GetTencentLocationVO.ResultBean.LocationBean();
        locationBean.setLat(lat);
        locationBean.setLng(lng);
        resultBean.setLocation(locationBean);
        GetTencentLocationVO locationVO = new GetTencentLocationVO();
        locationVO.setResult(resultBean);
        return new ResponseEntity<>(locationVO, HttpStatus.CREATED);
    }


    @PostMapping
    @Log("新增门店")
    @ApiOperation("新增门店")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxSystemStore resources){
        resources.setAuditStatus(0);
        resources.setRejectcause(null);
        resources.setStoreMoney(new BigDecimal(0.00));
        resources.setSettleAccounts(0);
        if(resources.getRetailType().equals("0")){
            resources.setPermit(1);
        }else{
            resources.setPermit(0);
        }
        if(!yxSystemStoreService.save(resources)){
            throw new UtilException("该手机号已绑定门店");
        }
        return new ResponseEntity<>(true,HttpStatus.CREATED);
    }


    @PutMapping
    @Log("修改门店")
    @ApiOperation("修改门店")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxSystemStore resources){
        yxSystemStoreService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除门店")
    @ApiOperation("删除门店")
    @PreAuthorize("hasAnyAuthority('yxSystemStore:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxSystemStoreService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("门店审核")
    @ApiOperation("门店审核")
    @GetMapping("/audit")
    public ResponseEntity<Object> audit( Integer id) {
        yxSystemStoreService.auditByIds(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    @Log("门店审核驳回")
    @ApiOperation("门店审核驳回")
    @PostMapping("/rejectSubmit")
    public ResponseEntity<Object> rejectSubmit(@RequestBody YxSystemStore resources) {
        yxSystemStoreService.reject(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    @Log("是否允许向平台进货")
    @ApiOperation("是否允许向平台进货")
    @PostMapping("/UpdatePermit")
    public ResponseEntity<Object> UpdatePermit(@RequestBody YxSystemStore resources) {
        yxSystemStoreService.permit(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
